<template>
  <view>
    <pageModule
      hasLink
      pageTitle="请登录"
      btnText="登录"
      :type="form.login_type"
      @changeType="(e) => (form.login_type = e)"
      @submit="submit"
      :disabled="!form.phone"
    >
      <template #header>
        <pageInp
          label="账户名"
          placeholder="请输入手机号"
          inputType="tel"
          :value.sync="form.phone"
        ></pageInp>
        <pageInp
          v-if="!form.login_type"
          label="密码"
          placeholder="请输入密码"
          inputType="password"
          :value.sync="form.password"
        ></pageInp>
        <pageInp
          v-else
          isGetCode
          label="验证码"
          placeholder="请输入验证码"
          :phone="form.phone"
          :value.sync="form.verify_code"
          code_type="login"
        ></pageInp>
      </template>
    </pageModule>
    <pagePrivacy ref="pagePrivacy"></pagePrivacy>
  </view>
</template>

<script>
import pageModule from "./page-module";
import pageInp from "./page-inp";
import pagePrivacy from "./page-privacy.vue";
import { regMobile } from "@/common/regExp";

export default {
  components: {
    pageModule,
    pageInp,
    pagePrivacy
  },

  data() {
    return {
      form: {
        phone: "",
        verify_code: "",
        login_type: 0,
        password: "",
		active:false
      }
    };
  },

  methods: {
    async submit() {
      try {
        let code = "",
          { form } = this,
          share_user_id = uni.getStorageSync("parent_user_id") || "";
          
        if (!form.phone) return this.$u.toast("请输入手机号");
        if (!regMobile(form.phone))
          return this.$utils.toast("请填写正确的手机号~");

        if (form.login_type) {
          if (!form.verify_code) return this.$u.toast("请输入验证码");
        } else {
          if (!form.password) return this.$u.toast("请输入密码");
        }

        if (!this.$refs.pagePrivacy.hasChecked()) return;

        // #ifdef MP
        const provider = (await uni.getProvider({ service: "oauth" }))[1]
          .provider[0];
        code = (
          await uni.login({
            provider
          })
        )[1].code;
        // #endif

        const res = await this.$allrequest.user.user_login(
          { ...form, share_user_id, _platform: "phone", code },
          true
        );

        if (!res.code) {
          this.afterLogin(res);
        }
      } catch (error) {
        this.$u.toast(error);
      }
    },

    afterLogin(result) {
      uni.setStorageSync("token", result.data.access_token);
      uni.removeStorageSync("storeMdData");

      uni.showToast({
        title: result.msg,
        duration: 1000,
        icon: "none"
      });
      setTimeout(() => {
        uni.setStorageSync("userInfo", result.data);
        this.$store.commit("setUserInfo", result.data);
        // this.$utils.toUrl("/pages/my/my", "switchtab");
        this.loginBack();
      }, 1000);
    },
    async loginBack() {
	  this.active=uni.getStorageSync("mch_id") || 0;
      let hasOneStroe = false;
      try {
        const res = await getStoreSwitch();
        if (res.data.is_switch_store) {
          uni.removeStorageSync("storeMdData");
          this.$store.commit("setStoreId", res.data.store_id);
          this.$store.commit("setCopyright", res.data.copyright);
          hasOneStroe = true;
          if (this.routeType) this.routeType = "reLaunch";
        }
      } catch (error) {}
      if (this.noLoginTabarPath && this.routeType) {
        this.$utils.toUrl(this.noLoginTabarPath, this.routeType);
        setTimeout(() => {
          this.$store.commit("setNoLoginTabarPath", "");
          this.$store.commit("setRouteType", "");
        }, 0);
      } else {
		  console.log('我走到了333333')
        if (hasOneStroe) return this.$utils.toUrl("/pages/my/my", "reLaunch");
		if(this.active){
			this.$utils.toUrl("/mch_store/mchCommodity/mchCommodity", "reLaunch");
			return 
		} 
        this.$utils.goBack();
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
